package com.obelisco.vista.controles;

import java.util.ArrayList;
import java.util.List;

import nextapp.echo2.app.Extent;

import com.jgoodies.binding.adapter.echo.ComboBoxAdapter;
import com.jgoodies.binding.adapter.echo.SingleListSelectionAdapter;
import com.jgoodies.binding.beans.PropertyAdapter;
import com.jgoodies.binding.list.echo.SelectionInList;

import echopointng.SelectFieldEx;

public class BindingSelectField extends SelectField {

	// private ValueModel model = null;
	private PropertyAdapter model = null;
	
	private ComboBoxAdapter adapter = null;

	private SelectionInList selectionInList = null;

	private int ancho;

	public BindingSelectField(int ancho) {
		super(ancho);
		// TODO Auto-generated constructor stub
		this.ancho = ancho;
		initComponents();
	}

	private void initComponents() {		
		setStyleName("Obelisco.SelectField");
		setWidth(new Extent(ancho));
	}

	public BindingSelectField(int ancho, List lista, Object object, String property) {
		this(ancho);
		if (model == null) {
			model = new PropertyAdapter(object, property, true);
		} else {
			model.setBean(object);
		}
		selectionInList = new SelectionInList(lista, model);
		setModel(selectionInList);
		setSelectionModel(new SingleListSelectionAdapter(selectionInList.getSelectionIndexHolder()));
	}

	public void setBinding(List lista, Object object, String property) {
		this.setItems(lista);
		if (model == null) {
			model = new PropertyAdapter(object, property, true);
		} else {
			model.setBean(object);
		}
		selectionInList = new SelectionInList(lista, model);		
		setModel(selectionInList);
		setSelectionModel(new SingleListSelectionAdapter(selectionInList.getSelectionIndexHolder()));
	}

	public void setItems(List lista) {		
		selectionInList = new SelectionInList(lista);		
		setModel(selectionInList);
		setSelectionModel(new SingleListSelectionAdapter(selectionInList.getSelectionIndexHolder()));
	}

	public List getItems()
	{
		List items = new ArrayList();
		
		for(int i = 0; i < selectionInList.size(); i++)
		{
			items.add(selectionInList.get(i));
		}
		return items;
	}
}
